document.addEventListener('DOMContentLoaded', function() {
    // 平滑滚动
    document.querySelectorAll('a[href^="#"]').forEach(anchor => {
        anchor.addEventListener('click', function (e) {
            e.preventDefault();
            document.querySelector(this.getAttribute('href')).scrollIntoView({
                behavior: 'smooth'
            });
        });
    });

    // 博客搜索功能
    const searchInput = document.getElementById('search-posts');
    const categoryFilter = document.getElementById('category-filter');
    
    if (searchInput && categoryFilter) {
        const postItems = document.querySelectorAll('.post-item');
        
        searchInput.addEventListener('input', filterPosts);
        categoryFilter.addEventListener('change', filterPosts);
        
        function filterPosts() {
            const searchTerm = searchInput.value.toLowerCase();
            const categoryTerm = categoryFilter.value.toLowerCase();
            
            postItems.forEach(post => {
                const title = post.querySelector('h3').textContent.toLowerCase();
                const excerpt = post.querySelector('.post-excerpt').textContent.toLowerCase();
                const category = post.querySelector('.post-category').textContent.toLowerCase();
                
                const matchesSearch = title.includes(searchTerm) || excerpt.includes(searchTerm);
                const matchesCategory = categoryTerm === '' || category === categoryTerm;
                
                if (matchesSearch && matchesCategory) {
                    post.style.display = 'grid';
                } else {
                    post.style.display = 'none';
                }
            });
        }
    }

    // 联系表单验证
    const contactForm = document.getElementById('contact-form');
    if (contactForm) {
        contactForm.addEventListener('submit', function(e) {
            e.preventDefault();
            
            // 获取表单字段
            const name = document.getElementById('name').value;
            const email = document.getElementById('email').value;
            const subject = document.getElementById('subject').value;
            const message = document.getElementById('message').value;
            
            // 简单验证
            if (name.trim() === '' || email.trim() === '' || subject.trim() === '' || message.trim() === '') {
                alert('请填写所有必填字段');
                return;
            }
            
            // 验证邮箱格式
            const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
            if (!emailRegex.test(email)) {
                alert('请输入有效的邮箱地址');
                return;
            }
            
            // 模拟表单提交
            alert('消息已成功发送！我们会尽快回复您。');
            contactForm.reset();
        });
    }

    // 页面加载动画
    const body = document.body;
    body.classList.add('loaded');

    // 响应式导航菜单切换
    const createMobileMenu = () => {
        const header = document.querySelector('header');
        if (!header) return;
        
        const nav = header.querySelector('nav');
        const container = header.querySelector('.container');
        
        // 创建汉堡菜单按钮
        const menuToggle = document.createElement('button');
        menuToggle.classList.add('menu-toggle');
        menuToggle.innerHTML = '<span></span><span></span><span></span>';
        container.appendChild(menuToggle);
        
        // 添加切换类
        menuToggle.addEventListener('click', () => {
            nav.classList.toggle('active');
            menuToggle.classList.toggle('active');
        });
        
        // 点击菜单项后关闭导航
        const navLinks = nav.querySelectorAll('a');
        navLinks.forEach(link => {
            link.addEventListener('click', () => {
                nav.classList.remove('active');
                menuToggle.classList.remove('active');
            });
        });
    };
    
    // 只在小屏幕上创建移动导航
    if (window.innerWidth <= 768) {
        createMobileMenu();
    }
    
    // 监听窗口大小变化
    window.addEventListener('resize', () => {
        const existing = document.querySelector('.menu-toggle');
        if (window.innerWidth <= 768 && !existing) {
            createMobileMenu();
        } else if (window.innerWidth > 768 && existing) {
            existing.remove();
            document.querySelector('nav').classList.remove('active');
        }
    });

    // 图片延迟加载
    if ('IntersectionObserver' in window) {
        const lazyImages = document.querySelectorAll('img[data-src]');
        const imageObserver = new IntersectionObserver((entries, observer) => {
            entries.forEach(entry => {
                if (entry.isIntersecting) {
                    const img = entry.target;
                    img.src = img.dataset.src;
                    img.removeAttribute('data-src');
                    imageObserver.unobserve(img);
                }
            });
        });

        lazyImages.forEach(img => {
            imageObserver.observe(img);
        });
    }

    // 滚动到顶部按钮
    const createScrollToTopButton = () => {
        const button = document.createElement('button');
        button.classList.add('scroll-top');
        button.innerHTML = '↑';
        document.body.appendChild(button);
        
        // 滚动事件监听
        window.addEventListener('scroll', () => {
            if (window.pageYOffset > 300) {
                button.classList.add('show');
            } else {
                button.classList.remove('show');
            }
        });
        
        // 点击事件
        button.addEventListener('click', () => {
            window.scrollTo({
                top: 0,
                behavior: 'smooth'
            });
        });
    };
    
    createScrollToTopButton();
}); 